package com.shuqi.model;

import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.util.Log;

import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.HttpCommonParamsUtils;
import com.shuqi.model.manager.MyAccountManager;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.PurchaseHistoryParser;

/**
 * 购入章节
 * 
 * @author zhuchen
 * 
 */
public class PayChapterHistoryModel extends ModelBase implements HttpRequestListener {

    private HttpRequest httpRequest;
    private Context context;

    private int pageIndex;

    private int pageCount = 20;

    private String bookId;

    public PayChapterHistoryModel(Context context) {
        this.context = context;
    }

    @Override
    public void action(int actionCode, Object object) {
        // TODO Auto-generated method stub
        fireEvent(actionCode, object);
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        return HttpCommonParamsUtils.getShuQiBasicParams(MyAccountManager.getInstance(context)
                .getUserId());
    }

    @Override
    public String getRequestUrl() {
        // TODO Auto-generated method stub
        Log.e("zc", "id==" + MyAccountManager.getInstance(context).getUserId());
        return Urls.getPayChapterHistoryUrl(MyAccountManager.getInstance(context).getUserId(),
                pageIndex, pageCount, bookId);
    }

    @Override
    public void loadData(Object... objs) {
        // TODO Auto-generated method stub
        this.pageIndex = (Integer) objs[0];
        this.bookId = (String) objs[1];
        this.httpRequest =
                new HttpRequest(context, UrlManager.COLUMN_DOMAIN_READ, getRequestUrl(),
                        setParams(), this);
        this.httpRequest.setDataParseAdapter(new PurchaseHistoryParser());
        MyTask.runInBackground(httpRequest, true);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        if (httpRequest != null) {
            httpRequest.abort();
        }
    }

}
